
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.forms.models &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-forms-models">
            
  <h1>Source code for django.forms.models</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Helper functions for creating Form classes from Django models</span>
<span class="sd">and database field objects.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">chain</span>

<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">NON_FIELD_ERRORS</span><span class="p">,</span> <span class="n">FieldError</span><span class="p">,</span> <span class="n">ImproperlyConfigured</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.forms.fields</span> <span class="k">import</span> <span class="n">ChoiceField</span><span class="p">,</span> <span class="n">Field</span>
<span class="kn">from</span> <span class="nn">django.forms.forms</span> <span class="k">import</span> <span class="n">BaseForm</span><span class="p">,</span> <span class="n">DeclarativeFieldsMetaclass</span>
<span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="k">import</span> <span class="n">BaseFormSet</span><span class="p">,</span> <span class="n">formset_factory</span>
<span class="kn">from</span> <span class="nn">django.forms.utils</span> <span class="k">import</span> <span class="n">ErrorList</span>
<span class="kn">from</span> <span class="nn">django.forms.widgets</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">HiddenInput</span><span class="p">,</span> <span class="n">MultipleHiddenInput</span><span class="p">,</span> <span class="n">SelectMultiple</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">capfirst</span><span class="p">,</span> <span class="n">get_text_list</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext</span><span class="p">,</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s1">&#39;ModelForm&#39;</span><span class="p">,</span> <span class="s1">&#39;BaseModelForm&#39;</span><span class="p">,</span> <span class="s1">&#39;model_to_dict&#39;</span><span class="p">,</span> <span class="s1">&#39;fields_for_model&#39;</span><span class="p">,</span>
    <span class="s1">&#39;ModelChoiceField&#39;</span><span class="p">,</span> <span class="s1">&#39;ModelMultipleChoiceField&#39;</span><span class="p">,</span> <span class="s1">&#39;ALL_FIELDS&#39;</span><span class="p">,</span>
    <span class="s1">&#39;BaseModelFormSet&#39;</span><span class="p">,</span> <span class="s1">&#39;modelformset_factory&#39;</span><span class="p">,</span> <span class="s1">&#39;BaseInlineFormSet&#39;</span><span class="p">,</span>
    <span class="s1">&#39;inlineformset_factory&#39;</span><span class="p">,</span> <span class="s1">&#39;modelform_factory&#39;</span><span class="p">,</span>
<span class="p">)</span>

<span class="n">ALL_FIELDS</span> <span class="o">=</span> <span class="s1">&#39;__all__&#39;</span>


<span class="k">def</span> <span class="nf">construct_instance</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Constructs and returns a model instance from the bound ``form``&#39;s</span>
<span class="sd">    ``cleaned_data``, but does not save the returned instance to the</span>
<span class="sd">    database.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
    <span class="n">opts</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span>

    <span class="n">cleaned_data</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span>
    <span class="n">file_field_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">editable</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">AutoField</span><span class="p">)</span> \
                <span class="ow">or</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cleaned_data</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="c1"># Leave defaults for fields that aren&#39;t in POST data, except for</span>
        <span class="c1"># checkbox inputs because they don&#39;t appear in POST data if not checked.</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">has_default</span><span class="p">()</span> <span class="ow">and</span>
                <span class="n">form</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">value_omitted_from_data</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">files</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">))):</span>
            <span class="k">continue</span>
        <span class="c1"># Defer saving file-type fields until after the other fields, so a</span>
        <span class="c1"># callable upload_to can use the values from other fields.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">FileField</span><span class="p">):</span>
            <span class="n">file_field_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">save_form_data</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">cleaned_data</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>

    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">file_field_list</span><span class="p">:</span>
        <span class="n">f</span><span class="o">.</span><span class="n">save_form_data</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">cleaned_data</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>

    <span class="k">return</span> <span class="n">instance</span>


<span class="c1"># ModelForms #################################################################</span>

<span class="k">def</span> <span class="nf">model_to_dict</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a dict containing the data in ``instance`` suitable for passing as</span>
<span class="sd">    a Form&#39;s ``initial`` keyword argument.</span>

<span class="sd">    ``fields`` is an optional list of field names. If provided, only the named</span>
<span class="sd">    fields will be included in the returned dict.</span>

<span class="sd">    ``exclude`` is an optional list of field names. If provided, the named</span>
<span class="sd">    fields will be excluded from the returned dict, even if they are listed in</span>
<span class="sd">    the ``fields`` argument.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
    <span class="n">opts</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span>
    <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">chain</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">private_fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;editable&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">data</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">value_from_object</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
        <span class="c1"># Evaluate ManyToManyField QuerySets to prevent subsequent model</span>
        <span class="c1"># alteration of that field from being reflected in the data.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">):</span>
            <span class="n">data</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">data</span>


<span class="k">def</span> <span class="nf">apply_limit_choices_to_to_formfield</span><span class="p">(</span><span class="n">formfield</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Apply limit_choices_to to the formfield&#39;s queryset if needed.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">formfield</span><span class="p">,</span> <span class="s1">&#39;queryset&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">formfield</span><span class="p">,</span> <span class="s1">&#39;get_limit_choices_to&#39;</span><span class="p">):</span>
        <span class="n">limit_choices_to</span> <span class="o">=</span> <span class="n">formfield</span><span class="o">.</span><span class="n">get_limit_choices_to</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">limit_choices_to</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">formfield</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="n">formfield</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">complex_filter</span><span class="p">(</span><span class="n">limit_choices_to</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">fields_for_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">widgets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                     <span class="n">formfield_callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">localized_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                     <span class="n">labels</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">help_texts</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error_messages</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                     <span class="n">field_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">apply_limit_choices_to</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a ``OrderedDict`` containing form fields for the given model.</span>

<span class="sd">    ``fields`` is an optional list of field names. If provided, only the named</span>
<span class="sd">    fields will be included in the returned fields.</span>

<span class="sd">    ``exclude`` is an optional list of field names. If provided, the named</span>
<span class="sd">    fields will be excluded from the returned fields, even if they are listed</span>
<span class="sd">    in the ``fields`` argument.</span>

<span class="sd">    ``widgets`` is a dictionary of model field names mapped to a widget.</span>

<span class="sd">    ``formfield_callback`` is a callable that takes a model field and returns</span>
<span class="sd">    a form field.</span>

<span class="sd">    ``localized_fields`` is a list of names of fields which should be localized.</span>

<span class="sd">    ``labels`` is a dictionary of model field names mapped to a label.</span>

<span class="sd">    ``help_texts`` is a dictionary of model field names mapped to a help text.</span>

<span class="sd">    ``error_messages`` is a dictionary of model field names mapped to a</span>
<span class="sd">    dictionary of error messages.</span>

<span class="sd">    ``field_classes`` is a dictionary of model field names mapped to a form</span>
<span class="sd">    field class.</span>

<span class="sd">    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to</span>
<span class="sd">    should be applied to a field&#39;s queryset.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">field_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">ignored</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
    <span class="c1"># Avoid circular import</span>
    <span class="kn">from</span> <span class="nn">django.db.models.fields</span> <span class="k">import</span> <span class="n">Field</span> <span class="k">as</span> <span class="n">ModelField</span>
    <span class="n">sortable_private_fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">private_fields</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">ModelField</span><span class="p">)]</span>
    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">chain</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">,</span> <span class="n">sortable_private_fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">)):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;editable&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">fields</span> <span class="ow">and</span>
                    <span class="p">(</span><span class="n">exclude</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">)):</span>
                <span class="k">raise</span> <span class="n">FieldError</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; cannot be specified for </span><span class="si">%s</span><span class="s2"> model form as it is a non-editable field&quot;</span> <span class="o">%</span> <span class="p">(</span>
                        <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
            <span class="k">continue</span>

        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="n">widgets</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">widgets</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">localized_fields</span> <span class="o">==</span> <span class="n">ALL_FIELDS</span> <span class="ow">or</span> <span class="p">(</span><span class="n">localized_fields</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">localized_fields</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;localize&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">labels</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">labels</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">help_texts</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">help_texts</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;help_text&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">help_texts</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">error_messages</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">error_messages</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error_messages&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">error_messages</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">field_classes</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">field_classes</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;form_class&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">field_classes</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">formfield_callback</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">formfield</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">callable</span><span class="p">(</span><span class="n">formfield_callback</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;formfield_callback must be a function or callable&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">formfield</span> <span class="o">=</span> <span class="n">formfield_callback</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">formfield</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">apply_limit_choices_to</span><span class="p">:</span>
                <span class="n">apply_limit_choices_to_to_formfield</span><span class="p">(</span><span class="n">formfield</span><span class="p">)</span>
            <span class="n">field_list</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">formfield</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ignored</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
    <span class="n">field_dict</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">(</span><span class="n">field_list</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">fields</span><span class="p">:</span>
        <span class="n">field_dict</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">(</span>
            <span class="p">[(</span><span class="n">f</span><span class="p">,</span> <span class="n">field_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">f</span><span class="p">))</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span>
                <span class="k">if</span> <span class="p">((</span><span class="ow">not</span> <span class="n">exclude</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">exclude</span> <span class="ow">and</span> <span class="n">f</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">))</span> <span class="ow">and</span> <span class="p">(</span><span class="n">f</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">ignored</span><span class="p">)]</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="n">field_dict</span>


<span class="k">class</span> <span class="nc">ModelFormOptions</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fields</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exclude</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;exclude&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localized_fields</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;localized_fields&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">labels</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;labels&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">help_texts</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;help_texts&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;error_messages&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field_classes</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s1">&#39;field_classes&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ModelFormMetaclass</span><span class="p">(</span><span class="n">DeclarativeFieldsMetaclass</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">base_formfield_callback</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bases</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="s1">&#39;Meta&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">Meta</span><span class="p">,</span> <span class="s1">&#39;formfield_callback&#39;</span><span class="p">):</span>
                <span class="n">base_formfield_callback</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">Meta</span><span class="o">.</span><span class="n">formfield_callback</span>
                <span class="k">break</span>

        <span class="n">formfield_callback</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;formfield_callback&#39;</span><span class="p">,</span> <span class="n">base_formfield_callback</span><span class="p">)</span>

        <span class="n">new_class</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ModelFormMetaclass</span><span class="p">,</span> <span class="n">mcs</span><span class="p">)</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">bases</span> <span class="o">==</span> <span class="p">(</span><span class="n">BaseModelForm</span><span class="p">,):</span>
            <span class="k">return</span> <span class="n">new_class</span>

        <span class="n">opts</span> <span class="o">=</span> <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span> <span class="o">=</span> <span class="n">ModelFormOptions</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">new_class</span><span class="p">,</span> <span class="s1">&#39;Meta&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>

        <span class="c1"># We check if a string was passed to `fields` or `exclude`,</span>
        <span class="c1"># which is likely to be a mistake where the user typed (&#39;foo&#39;) instead</span>
        <span class="c1"># of (&#39;foo&#39;,)</span>
        <span class="k">for</span> <span class="n">opt</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="s1">&#39;exclude&#39;</span><span class="p">,</span> <span class="s1">&#39;localized_fields&#39;</span><span class="p">]:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="n">opt</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="ow">and</span> <span class="n">value</span> <span class="o">!=</span> <span class="n">ALL_FIELDS</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%(model)s</span><span class="s2">.Meta.</span><span class="si">%(opt)s</span><span class="s2"> cannot be a string. &quot;</span>
                       <span class="s2">&quot;Did you mean to type: (&#39;</span><span class="si">%(value)s</span><span class="s2">&#39;,)?&quot;</span> <span class="o">%</span> <span class="p">{</span>
                           <span class="s1">&#39;model&#39;</span><span class="p">:</span> <span class="n">new_class</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                           <span class="s1">&#39;opt&#39;</span><span class="p">:</span> <span class="n">opt</span><span class="p">,</span>
                           <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">,</span>
                       <span class="p">})</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">model</span><span class="p">:</span>
            <span class="c1"># If a model is defined, extract form fields from it.</span>
            <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">opts</span><span class="o">.</span><span class="n">exclude</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                    <span class="s2">&quot;Creating a ModelForm without either the &#39;fields&#39; attribute &quot;</span>
                    <span class="s2">&quot;or the &#39;exclude&#39; attribute is prohibited; form </span><span class="si">%s</span><span class="s2"> &quot;</span>
                    <span class="s2">&quot;needs updating.&quot;</span> <span class="o">%</span> <span class="n">name</span>
                <span class="p">)</span>

            <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span> <span class="o">==</span> <span class="n">ALL_FIELDS</span><span class="p">:</span>
                <span class="c1"># Sentinel for fields_for_model to indicate &quot;get the list of</span>
                <span class="c1"># fields from the model&quot;</span>
                <span class="n">opts</span><span class="o">.</span><span class="n">fields</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="n">fields</span> <span class="o">=</span> <span class="n">fields_for_model</span><span class="p">(</span>
                <span class="n">opts</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">exclude</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">widgets</span><span class="p">,</span>
                <span class="n">formfield_callback</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">localized_fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">labels</span><span class="p">,</span>
                <span class="n">opts</span><span class="o">.</span><span class="n">help_texts</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">error_messages</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">field_classes</span><span class="p">,</span>
                <span class="c1"># limit_choices_to will be applied during ModelForm.__init__().</span>
                <span class="n">apply_limit_choices_to</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="c1"># make sure opts.fields doesn&#39;t specify an invalid field</span>
            <span class="n">none_model_fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">v</span><span class="p">]</span>
            <span class="n">missing_fields</span> <span class="o">=</span> <span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">none_model_fields</span><span class="p">)</span> <span class="o">-</span>
                              <span class="nb">set</span><span class="p">(</span><span class="n">new_class</span><span class="o">.</span><span class="n">declared_fields</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>
            <span class="k">if</span> <span class="n">missing_fields</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;Unknown field(s) (</span><span class="si">%s</span><span class="s1">) specified for </span><span class="si">%s</span><span class="s1">&#39;</span>
                <span class="n">message</span> <span class="o">=</span> <span class="n">message</span> <span class="o">%</span> <span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">missing_fields</span><span class="p">),</span>
                                     <span class="n">opts</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
                <span class="k">raise</span> <span class="n">FieldError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
            <span class="c1"># Override default model fields with any custom declared ones</span>
            <span class="c1"># (plus, include all the other declared fields).</span>
            <span class="n">fields</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">new_class</span><span class="o">.</span><span class="n">declared_fields</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">new_class</span><span class="o">.</span><span class="n">declared_fields</span>

        <span class="n">new_class</span><span class="o">.</span><span class="n">base_fields</span> <span class="o">=</span> <span class="n">fields</span>

        <span class="k">return</span> <span class="n">new_class</span>


<span class="k">class</span> <span class="nc">BaseModelForm</span><span class="p">(</span><span class="n">BaseForm</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="s1">&#39;id_</span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">initial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error_class</span><span class="o">=</span><span class="n">ErrorList</span><span class="p">,</span> <span class="n">label_suffix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">empty_permitted</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">use_required_attribute</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;ModelForm has no model class specified.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># if we didn&#39;t get an instance, instantiate a new one</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">model</span><span class="p">()</span>
            <span class="n">object_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">instance</span>
            <span class="n">object_data</span> <span class="o">=</span> <span class="n">model_to_dict</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">exclude</span><span class="p">)</span>
        <span class="c1"># if initial was provided, it should override the values from instance</span>
        <span class="k">if</span> <span class="n">initial</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">object_data</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">initial</span><span class="p">)</span>
        <span class="c1"># self._validate_unique will be set to True by BaseModelForm.clean().</span>
        <span class="c1"># It is False by default so overriding self.clean() and failing to call</span>
        <span class="c1"># super will stop validate_unique from being called.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_validate_unique</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BaseModelForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">auto_id</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">object_data</span><span class="p">,</span> <span class="n">error_class</span><span class="p">,</span>
            <span class="n">label_suffix</span><span class="p">,</span> <span class="n">empty_permitted</span><span class="p">,</span> <span class="n">use_required_attribute</span><span class="o">=</span><span class="n">use_required_attribute</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">for</span> <span class="n">formfield</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">apply_limit_choices_to_to_formfield</span><span class="p">(</span><span class="n">formfield</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_validation_exclusions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        For backwards-compatibility, several types of fields need to be</span>
<span class="sd">        excluded from model validation. See the following tickets for</span>
<span class="sd">        details: #12507, #12521, #12553</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Build up a list of fields that should be excluded from model field</span>
        <span class="c1"># validation and unique checks.</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span>
            <span class="c1"># Exclude fields that aren&#39;t on the form. The developer may be</span>
            <span class="c1"># adding these values to the model after form validation.</span>
            <span class="k">if</span> <span class="n">field</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

            <span class="c1"># Don&#39;t perform model validation on fields that were defined</span>
            <span class="c1"># manually on the form and excluded via the ModelForm&#39;s Meta</span>
            <span class="c1"># class. See #12901.</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span> <span class="ow">and</span> <span class="n">field</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span> <span class="ow">and</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span><span class="p">:</span>
                <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

            <span class="c1"># Exclude fields that failed form validation. There&#39;s no need for</span>
            <span class="c1"># the model fields to validate them as well.</span>
            <span class="k">elif</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

            <span class="c1"># Exclude empty fields that are not required by the form, if the</span>
            <span class="c1"># underlying model field is required. This keeps the model field</span>
            <span class="c1"># from raising a required error. Note: don&#39;t exclude the field from</span>
            <span class="c1"># validation if the model field allows blanks. If it does, the blank</span>
            <span class="c1"># value may be included in a unique check, so cannot be excluded</span>
            <span class="c1"># from validation.</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">form_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                <span class="n">field_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">blank</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">form_field</span><span class="o">.</span><span class="n">required</span> <span class="ow">and</span> <span class="n">field_value</span> <span class="ow">in</span> <span class="n">form_field</span><span class="o">.</span><span class="n">empty_values</span><span class="p">:</span>
                    <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">exclude</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_validate_unique</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span>

    <span class="k">def</span> <span class="nf">_update_errors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errors</span><span class="p">):</span>
        <span class="c1"># Override any validation error messages defined at the model level</span>
        <span class="c1"># with those defined at the form level.</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span>

        <span class="c1"># Allow the model generated by construct_instance() to raise</span>
        <span class="c1"># ValidationError and have them handled in the same way as others.</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">errors</span><span class="p">,</span> <span class="s1">&#39;error_dict&#39;</span><span class="p">):</span>
            <span class="n">error_dict</span> <span class="o">=</span> <span class="n">errors</span><span class="o">.</span><span class="n">error_dict</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">error_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">NON_FIELD_ERRORS</span><span class="p">:</span> <span class="n">errors</span><span class="p">}</span>

        <span class="k">for</span> <span class="n">field</span><span class="p">,</span> <span class="n">messages</span> <span class="ow">in</span> <span class="n">error_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">field</span> <span class="o">==</span> <span class="n">NON_FIELD_ERRORS</span> <span class="ow">and</span> <span class="n">opts</span><span class="o">.</span><span class="n">error_messages</span> <span class="ow">and</span>
                    <span class="n">NON_FIELD_ERRORS</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">error_messages</span><span class="p">):</span>
                <span class="n">error_messages</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="n">NON_FIELD_ERRORS</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                <span class="n">error_messages</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="n">field</span><span class="p">]</span><span class="o">.</span><span class="n">error_messages</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="k">for</span> <span class="n">message</span> <span class="ow">in</span> <span class="n">messages</span><span class="p">:</span>
                <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">)</span> <span class="ow">and</span>
                        <span class="n">message</span><span class="o">.</span><span class="n">code</span> <span class="ow">in</span> <span class="n">error_messages</span><span class="p">):</span>
                    <span class="n">message</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">error_messages</span><span class="p">[</span><span class="n">message</span><span class="o">.</span><span class="n">code</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_error</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_post_clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span>

        <span class="n">exclude</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_validation_exclusions</span><span class="p">()</span>

        <span class="c1"># Foreign Keys being used to represent inline relationships</span>
        <span class="c1"># are excluded from basic field value validation. This is for two</span>
        <span class="c1"># reasons: firstly, the value may not be supplied (#12507; the</span>
        <span class="c1"># case of providing new values to the admin); secondly the</span>
        <span class="c1"># object being referred to may not yet fully exist (#12749).</span>
        <span class="c1"># However, these fields *must* be included in uniqueness checks,</span>
        <span class="c1"># so this can&#39;t be part of _get_validation_exclusions().</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">InlineForeignKeyField</span><span class="p">):</span>
                <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">construct_instance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">exclude</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_update_errors</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">full_clean</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">,</span> <span class="n">validate_unique</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_update_errors</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

        <span class="c1"># Validate uniqueness if needed.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate_unique</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">validate_unique</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">validate_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calls the instance&#39;s validate_unique() method and updates the form&#39;s</span>
<span class="sd">        validation errors if any were raised.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_validation_exclusions</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">validate_unique</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_update_errors</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_save_m2m</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save the many-to-many fields and generic relations for this form.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cleaned_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_meta</span>
        <span class="c1"># Note that for historical reasons we want to include also</span>
        <span class="c1"># private_fields here. (GenericRelation was previously a fake</span>
        <span class="c1"># m2m field).</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">chain</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">private_fields</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;save_form_data&#39;</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">fields</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">exclude</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">cleaned_data</span><span class="p">:</span>
                <span class="n">f</span><span class="o">.</span><span class="n">save_form_data</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">cleaned_data</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save this form&#39;s self.instance object if commit=True. Otherwise, add</span>
<span class="sd">        a save_m2m() method to the form which can be called after the instance</span>
<span class="sd">        is saved manually at a later time. Return the model instance.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The </span><span class="si">%s</span><span class="s2"> could not be </span><span class="si">%s</span><span class="s2"> because the data didn&#39;t validate.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                    <span class="s1">&#39;created&#39;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="k">else</span> <span class="s1">&#39;changed&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">commit</span><span class="p">:</span>
            <span class="c1"># If committing, save the instance and the m2m data immediately.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_save_m2m</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># If not committing, add a method to the form to allow deferred</span>
            <span class="c1"># saving of m2m data.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">save_m2m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_save_m2m</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span>

    <span class="n">save</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>


<div class="viewcode-block" id="ModelForm"><a class="viewcode-back" href="../../../topics/forms/modelforms.html#django.contrib.admin.ModelForm">[docs]</a><span class="k">class</span> <span class="nc">ModelForm</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">ModelFormMetaclass</span><span class="p">,</span> <span class="n">BaseModelForm</span><span class="p">)):</span>
    <span class="k">pass</span></div>


<div class="viewcode-block" id="modelform_factory"><a class="viewcode-back" href="../../../ref/forms/models.html#django.contrib.admin.modelform_factory">[docs]</a><span class="k">def</span> <span class="nf">modelform_factory</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="n">ModelForm</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                      <span class="n">formfield_callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">widgets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">localized_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                      <span class="n">labels</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">help_texts</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error_messages</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                      <span class="n">field_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a ModelForm containing form fields for the given model.</span>

<span class="sd">    ``fields`` is an optional list of field names. If provided, only the named</span>
<span class="sd">    fields will be included in the returned fields. If omitted or &#39;__all__&#39;,</span>
<span class="sd">    all fields will be used.</span>

<span class="sd">    ``exclude`` is an optional list of field names. If provided, the named</span>
<span class="sd">    fields will be excluded from the returned fields, even if they are listed</span>
<span class="sd">    in the ``fields`` argument.</span>

<span class="sd">    ``widgets`` is a dictionary of model field names mapped to a widget.</span>

<span class="sd">    ``localized_fields`` is a list of names of fields which should be localized.</span>

<span class="sd">    ``formfield_callback`` is a callable that takes a model field and returns</span>
<span class="sd">    a form field.</span>

<span class="sd">    ``labels`` is a dictionary of model field names mapped to a label.</span>

<span class="sd">    ``help_texts`` is a dictionary of model field names mapped to a help text.</span>

<span class="sd">    ``error_messages`` is a dictionary of model field names mapped to a</span>
<span class="sd">    dictionary of error messages.</span>

<span class="sd">    ``field_classes`` is a dictionary of model field names mapped to a form</span>
<span class="sd">    field class.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Create the inner Meta class. FIXME: ideally, we should be able to</span>
    <span class="c1"># construct a ModelForm without creating and passing in a temporary</span>
    <span class="c1"># inner class.</span>

    <span class="c1"># Build up a list of attributes that the Meta object will have.</span>
    <span class="n">attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;model&#39;</span><span class="p">:</span> <span class="n">model</span><span class="p">}</span>
    <span class="k">if</span> <span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fields</span>
    <span class="k">if</span> <span class="n">exclude</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;exclude&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">exclude</span>
    <span class="k">if</span> <span class="n">widgets</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;widgets&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span>
    <span class="k">if</span> <span class="n">localized_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;localized_fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">localized_fields</span>
    <span class="k">if</span> <span class="n">labels</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;labels&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">labels</span>
    <span class="k">if</span> <span class="n">help_texts</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;help_texts&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">help_texts</span>
    <span class="k">if</span> <span class="n">error_messages</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;error_messages&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">error_messages</span>
    <span class="k">if</span> <span class="n">field_classes</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;field_classes&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">field_classes</span>

    <span class="c1"># If parent form class already has an inner Meta, the Meta we&#39;re</span>
    <span class="c1"># creating needs to inherit from the parent&#39;s inner meta.</span>
    <span class="n">parent</span> <span class="o">=</span> <span class="p">(</span><span class="nb">object</span><span class="p">,)</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="s1">&#39;Meta&#39;</span><span class="p">):</span>
        <span class="n">parent</span> <span class="o">=</span> <span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">Meta</span><span class="p">,</span> <span class="nb">object</span><span class="p">)</span>
    <span class="n">Meta</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;Meta&#39;</span><span class="p">),</span> <span class="n">parent</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">formfield_callback</span><span class="p">:</span>
        <span class="n">Meta</span><span class="o">.</span><span class="n">formfield_callback</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">formfield_callback</span><span class="p">)</span>
    <span class="c1"># Give this new form class a reasonable name.</span>
    <span class="n">class_name</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;Form&#39;</span><span class="p">)</span>

    <span class="c1"># Class attributes for the new form class.</span>
    <span class="n">form_class_attrs</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;Meta&#39;</span><span class="p">:</span> <span class="n">Meta</span><span class="p">,</span>
        <span class="s1">&#39;formfield_callback&#39;</span><span class="p">:</span> <span class="n">formfield_callback</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">Meta</span><span class="p">,</span> <span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">Meta</span><span class="p">,</span> <span class="s1">&#39;exclude&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
            <span class="s2">&quot;Calling modelform_factory without defining &#39;fields&#39; or &quot;</span>
            <span class="s2">&quot;&#39;exclude&#39; explicitly is prohibited.&quot;</span>
        <span class="p">)</span>

    <span class="c1"># Instantiate type(form) in order to use the same metaclass as form.</span>
    <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="n">form</span><span class="p">)(</span><span class="n">class_name</span><span class="p">,</span> <span class="p">(</span><span class="n">form</span><span class="p">,),</span> <span class="n">form_class_attrs</span><span class="p">)</span></div>


<span class="c1"># ModelFormSets ##############################################################</span>

<span class="k">class</span> <span class="nc">BaseModelFormSet</span><span class="p">(</span><span class="n">BaseFormSet</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A ``FormSet`` for editing a queryset and/or adding new objects to it.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># Set of fields that must be unique among forms of this set.</span>
    <span class="n">unique_fields</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="s1">&#39;id_</span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">queryset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_extra</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;initial&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;files&#39;</span><span class="p">:</span> <span class="n">files</span><span class="p">,</span> <span class="s1">&#39;auto_id&#39;</span><span class="p">:</span> <span class="n">auto_id</span><span class="p">,</span> <span class="s1">&#39;prefix&#39;</span><span class="p">:</span> <span class="n">prefix</span><span class="p">}</span>
        <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">initial_form_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns the number of forms that are required in this FormSet.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">files</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">())</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">initial_form_count</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_existing_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pk</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_object_dict&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_object_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">o</span><span class="o">.</span><span class="n">pk</span><span class="p">:</span> <span class="n">o</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()}</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_object_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        If the field is a related field, fetch the concrete field&#39;s (that</span>
<span class="sd">        is, the ultimate pointed-to field&#39;s) to_python.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">while</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">get_related_field</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">field</span><span class="o">.</span><span class="n">to_python</span>

    <span class="k">def</span> <span class="nf">_construct_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_bound</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_form_count</span><span class="p">():</span>
            <span class="n">pk_key</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">-</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">pk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">pk_key</span><span class="p">]</span>
            <span class="n">pk_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span>
            <span class="n">to_python</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_to_python</span><span class="p">(</span><span class="n">pk_field</span><span class="p">)</span>
            <span class="n">pk</span> <span class="o">=</span> <span class="n">to_python</span><span class="p">(</span><span class="n">pk</span><span class="p">)</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;instance&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_existing_object</span><span class="p">(</span><span class="n">pk</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_form_count</span><span class="p">()</span> <span class="ow">and</span> <span class="s1">&#39;instance&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;instance&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()[</span><span class="n">i</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_form_count</span><span class="p">()</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_extra</span><span class="p">:</span>
            <span class="c1"># Set initial values for extra forms</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;initial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_extra</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_form_count</span><span class="p">()]</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_construct_form</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_queryset&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>

            <span class="c1"># If the queryset isn&#39;t already ordered we need to add an</span>
            <span class="c1"># artificial ordering here to make sure that all formsets</span>
            <span class="c1"># constructed from this queryset have the same form order.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">qs</span><span class="o">.</span><span class="n">ordered</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

            <span class="c1"># Removed queryset limiting here. As per discussion re: #13023</span>
            <span class="c1"># on django-dev, max_num should not prevent existing</span>
            <span class="c1"># related objects/inlines from being displayed.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_queryset</span> <span class="o">=</span> <span class="n">qs</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_queryset</span>

    <span class="k">def</span> <span class="nf">save_new</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Saves and returns a new model instance for the given form.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="n">commit</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">save_existing</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Saves and returns an existing model instance for the given form.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="n">commit</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">delete_existing</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Deletes an existing model instance.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">commit</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Saves model instances for every form, adding and changing instances</span>
<span class="sd">        as necessary, and returns the list of instances.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">commit</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">saved_forms</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="k">def</span> <span class="nf">save_m2m</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">saved_forms</span><span class="p">:</span>
                    <span class="n">form</span><span class="o">.</span><span class="n">save_m2m</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">save_m2m</span> <span class="o">=</span> <span class="n">save_m2m</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_existing_objects</span><span class="p">(</span><span class="n">commit</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_new_objects</span><span class="p">(</span><span class="n">commit</span><span class="p">)</span>

    <span class="n">save</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">validate_unique</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">validate_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Collect unique_checks and date_checks to run from all the forms.</span>
        <span class="n">all_unique_checks</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="n">all_date_checks</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="n">forms_to_delete</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">deleted_forms</span>
        <span class="n">valid_forms</span> <span class="o">=</span> <span class="p">[</span><span class="n">form</span> <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">forms</span> <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span> <span class="ow">and</span> <span class="n">form</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">forms_to_delete</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">valid_forms</span><span class="p">:</span>
            <span class="n">exclude</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">_get_validation_exclusions</span><span class="p">()</span>
            <span class="n">unique_checks</span><span class="p">,</span> <span class="n">date_checks</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_get_unique_checks</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">)</span>
            <span class="n">all_unique_checks</span> <span class="o">=</span> <span class="n">all_unique_checks</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">unique_checks</span><span class="p">))</span>
            <span class="n">all_date_checks</span> <span class="o">=</span> <span class="n">all_date_checks</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">date_checks</span><span class="p">))</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Do each of the unique checks (unique and unique_together)</span>
        <span class="k">for</span> <span class="n">uclass</span><span class="p">,</span> <span class="n">unique_check</span> <span class="ow">in</span> <span class="n">all_unique_checks</span><span class="p">:</span>
            <span class="n">seen_data</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">valid_forms</span><span class="p">:</span>
                <span class="c1"># Get the data for the set of fields that must be unique among the forms.</span>
                <span class="n">row_data</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">field</span> <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique_fields</span> <span class="k">else</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">unique_check</span> <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span>
                <span class="p">)</span>
                <span class="c1"># Reduce Model instances to their primary key values</span>
                <span class="n">row_data</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s1">&#39;_get_pk_val&#39;</span><span class="p">)</span> <span class="k">else</span> <span class="n">d</span>
                                 <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">row_data</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">row_data</span> <span class="ow">and</span> <span class="kc">None</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">row_data</span><span class="p">:</span>
                    <span class="c1"># if we&#39;ve already seen it then we have a uniqueness failure</span>
                    <span class="k">if</span> <span class="n">row_data</span> <span class="ow">in</span> <span class="n">seen_data</span><span class="p">:</span>
                        <span class="c1"># poke error messages into the right places and mark</span>
                        <span class="c1"># the form as invalid</span>
                        <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_unique_error_message</span><span class="p">(</span><span class="n">unique_check</span><span class="p">))</span>
                        <span class="n">form</span><span class="o">.</span><span class="n">_errors</span><span class="p">[</span><span class="n">NON_FIELD_ERRORS</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form_error</span><span class="p">()])</span>
                        <span class="c1"># remove the data from the cleaned_data dict since it was invalid</span>
                        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">unique_check</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">:</span>
                                <span class="k">del</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                    <span class="c1"># mark the data as seen</span>
                    <span class="n">seen_data</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">row_data</span><span class="p">)</span>
        <span class="c1"># iterate over each of the date checks now</span>
        <span class="k">for</span> <span class="n">date_check</span> <span class="ow">in</span> <span class="n">all_date_checks</span><span class="p">:</span>
            <span class="n">seen_data</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="n">uclass</span><span class="p">,</span> <span class="n">lookup</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">unique_for</span> <span class="o">=</span> <span class="n">date_check</span>
            <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">valid_forms</span><span class="p">:</span>
                <span class="c1"># see if we have data for both fields</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="ow">and</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span>
                        <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">unique_for</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                    <span class="c1"># if it&#39;s a date lookup we need to get the data for all the fields</span>
                    <span class="k">if</span> <span class="n">lookup</span> <span class="o">==</span> <span class="s1">&#39;date&#39;</span><span class="p">:</span>
                        <span class="n">date</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">unique_for</span><span class="p">]</span>
                        <span class="n">date_data</span> <span class="o">=</span> <span class="p">(</span><span class="n">date</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">date</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">date</span><span class="o">.</span><span class="n">day</span><span class="p">)</span>
                    <span class="c1"># otherwise it&#39;s just the attribute on the date/datetime</span>
                    <span class="c1"># object</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">date_data</span> <span class="o">=</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">unique_for</span><span class="p">],</span> <span class="n">lookup</span><span class="p">),)</span>
                    <span class="n">data</span> <span class="o">=</span> <span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">field</span><span class="p">],)</span> <span class="o">+</span> <span class="n">date_data</span>
                    <span class="c1"># if we&#39;ve already seen it then we have a uniqueness failure</span>
                    <span class="k">if</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">seen_data</span><span class="p">:</span>
                        <span class="c1"># poke error messages into the right places and mark</span>
                        <span class="c1"># the form as invalid</span>
                        <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_date_error_message</span><span class="p">(</span><span class="n">date_check</span><span class="p">))</span>
                        <span class="n">form</span><span class="o">.</span><span class="n">_errors</span><span class="p">[</span><span class="n">NON_FIELD_ERRORS</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form_error</span><span class="p">()])</span>
                        <span class="c1"># remove the data from the cleaned_data dict since it was invalid</span>
                        <span class="k">del</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                    <span class="c1"># mark the data as seen</span>
                    <span class="n">seen_data</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_unique_error_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unique_check</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">unique_check</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">ugettext</span><span class="p">(</span><span class="s2">&quot;Please correct the duplicate data for </span><span class="si">%(field)s</span><span class="s2">.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s2">&quot;field&quot;</span><span class="p">:</span> <span class="n">unique_check</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
            <span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">ugettext</span><span class="p">(</span><span class="s2">&quot;Please correct the duplicate data for </span><span class="si">%(field)s</span><span class="s2">, which must be unique.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s2">&quot;field&quot;</span><span class="p">:</span> <span class="n">get_text_list</span><span class="p">(</span><span class="n">unique_check</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;and&quot;</span><span class="p">))),</span>
            <span class="p">}</span>

    <span class="k">def</span> <span class="nf">get_date_error_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date_check</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">ugettext</span><span class="p">(</span>
            <span class="s2">&quot;Please correct the duplicate data for </span><span class="si">%(field_name)s</span><span class="s2"> &quot;</span>
            <span class="s2">&quot;which must be unique for the </span><span class="si">%(lookup)s</span><span class="s2"> in </span><span class="si">%(date_field)s</span><span class="s2">.&quot;</span>
        <span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;field_name&#39;</span><span class="p">:</span> <span class="n">date_check</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span>
            <span class="s1">&#39;date_field&#39;</span><span class="p">:</span> <span class="n">date_check</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
            <span class="s1">&#39;lookup&#39;</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">date_check</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">get_form_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">ugettext</span><span class="p">(</span><span class="s2">&quot;Please correct the duplicate values below.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">save_existing_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">changed_objects</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deleted_objects</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_forms</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="n">saved_instances</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">forms_to_delete</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">deleted_forms</span>
        <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_forms</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">instance</span>
            <span class="c1"># If the pk is None, it means either:</span>
            <span class="c1"># 1. The object is an unexpected empty model, created by invalid</span>
            <span class="c1">#    POST data such as an object outside the formset&#39;s queryset.</span>
            <span class="c1"># 2. The object was already deleted from the database.</span>
            <span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">forms_to_delete</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">deleted_objects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">delete_existing</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="n">commit</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">form</span><span class="o">.</span><span class="n">has_changed</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">changed_objects</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">changed_data</span><span class="p">))</span>
                <span class="n">saved_instances</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_existing</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="n">commit</span><span class="p">))</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">commit</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">saved_forms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">saved_instances</span>

    <span class="k">def</span> <span class="nf">save_new_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_objects</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_forms</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">form</span><span class="o">.</span><span class="n">has_changed</span><span class="p">():</span>
                <span class="k">continue</span>
            <span class="c1"># If someone has marked an add form for deletion, don&#39;t save the</span>
            <span class="c1"># object.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">can_delete</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_delete_form</span><span class="p">(</span><span class="n">form</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">new_objects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_new</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="n">commit</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">commit</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">saved_forms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_objects</span>

    <span class="k">def</span> <span class="nf">add_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a hidden field for the object&#39;s primary key.&quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.db.models</span> <span class="k">import</span> <span class="n">AutoField</span><span class="p">,</span> <span class="n">OneToOneField</span><span class="p">,</span> <span class="n">ForeignKey</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pk_field</span> <span class="o">=</span> <span class="n">pk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span>
        <span class="c1"># If a pk isn&#39;t editable, then it won&#39;t be on the form, so we need to</span>
        <span class="c1"># add it here so we can tell which object is which when we get the</span>
        <span class="c1"># data back. Generally, pk.editable should be false, but for some</span>
        <span class="c1"># reason, auto_created pk fields and AutoField&#39;s editable attribute is</span>
        <span class="c1"># True, so check for that as well.</span>

        <span class="k">def</span> <span class="nf">pk_is_not_editable</span><span class="p">(</span><span class="n">pk</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">(</span>
                <span class="p">(</span><span class="ow">not</span> <span class="n">pk</span><span class="o">.</span><span class="n">editable</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">pk</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pk</span><span class="p">,</span> <span class="n">AutoField</span><span class="p">))</span> <span class="ow">or</span> <span class="p">(</span>
                    <span class="n">pk</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">and</span> <span class="n">pk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">parent_link</span> <span class="ow">and</span>
                    <span class="n">pk_is_not_editable</span><span class="p">(</span><span class="n">pk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">pk_is_not_editable</span><span class="p">(</span><span class="n">pk</span><span class="p">)</span> <span class="ow">or</span> <span class="n">pk</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_bound</span><span class="p">:</span>
                <span class="c1"># If we&#39;re adding the related instance, ignore its primary key</span>
                <span class="c1"># as it could be an auto-generated default which isn&#39;t actually</span>
                <span class="c1"># in the database.</span>
                <span class="n">pk_value</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="k">else</span> <span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">pk</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">pk_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()[</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">pk</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">pk_value</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                    <span class="n">pk_value</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pk</span><span class="p">,</span> <span class="n">OneToOneField</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pk</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">):</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="n">pk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">widgets</span><span class="p">:</span>
                <span class="n">widget</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">widgets</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pk_field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">HiddenInput</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">widget</span> <span class="o">=</span> <span class="n">HiddenInput</span>
            <span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_pk_field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">ModelChoiceField</span><span class="p">(</span><span class="n">qs</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="n">pk_value</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="n">widget</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_fields</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>


<div class="viewcode-block" id="modelformset_factory"><a class="viewcode-back" href="../../../ref/forms/models.html#django.contrib.admin.modelformset_factory">[docs]</a><span class="k">def</span> <span class="nf">modelformset_factory</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="n">ModelForm</span><span class="p">,</span> <span class="n">formfield_callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                         <span class="n">formset</span><span class="o">=</span><span class="n">BaseModelFormSet</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">can_delete</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                         <span class="n">can_order</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">max_num</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                         <span class="n">widgets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">validate_max</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">localized_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                         <span class="n">labels</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">help_texts</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error_messages</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                         <span class="n">min_num</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">validate_min</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">field_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a FormSet class for the given Django model class.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">meta</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="s1">&#39;Meta&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="n">fields</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;exclude&#39;</span><span class="p">,</span> <span class="n">exclude</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
            <span class="s2">&quot;Calling modelformset_factory without defining &#39;fields&#39; or &quot;</span>
            <span class="s2">&quot;&#39;exclude&#39; explicitly is prohibited.&quot;</span>
        <span class="p">)</span>

    <span class="n">form</span> <span class="o">=</span> <span class="n">modelform_factory</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="n">fields</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">,</span>
                             <span class="n">formfield_callback</span><span class="o">=</span><span class="n">formfield_callback</span><span class="p">,</span>
                             <span class="n">widgets</span><span class="o">=</span><span class="n">widgets</span><span class="p">,</span> <span class="n">localized_fields</span><span class="o">=</span><span class="n">localized_fields</span><span class="p">,</span>
                             <span class="n">labels</span><span class="o">=</span><span class="n">labels</span><span class="p">,</span> <span class="n">help_texts</span><span class="o">=</span><span class="n">help_texts</span><span class="p">,</span>
                             <span class="n">error_messages</span><span class="o">=</span><span class="n">error_messages</span><span class="p">,</span> <span class="n">field_classes</span><span class="o">=</span><span class="n">field_classes</span><span class="p">)</span>
    <span class="n">FormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="n">extra</span><span class="p">,</span> <span class="n">min_num</span><span class="o">=</span><span class="n">min_num</span><span class="p">,</span> <span class="n">max_num</span><span class="o">=</span><span class="n">max_num</span><span class="p">,</span>
                              <span class="n">can_order</span><span class="o">=</span><span class="n">can_order</span><span class="p">,</span> <span class="n">can_delete</span><span class="o">=</span><span class="n">can_delete</span><span class="p">,</span>
                              <span class="n">validate_min</span><span class="o">=</span><span class="n">validate_min</span><span class="p">,</span> <span class="n">validate_max</span><span class="o">=</span><span class="n">validate_max</span><span class="p">)</span>
    <span class="n">FormSet</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
    <span class="k">return</span> <span class="n">FormSet</span></div>


<span class="c1"># InlineFormSets #############################################################</span>

<span class="k">class</span> <span class="nc">BaseInlineFormSet</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A formset for child objects related to a parent.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">save_as_new</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">instance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">save_as_new</span> <span class="o">=</span> <span class="n">save_as_new</span>
        <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">})</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">none</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unique_fields</span> <span class="o">=</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span><span class="p">}</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">,</span>
                                                <span class="n">queryset</span><span class="o">=</span><span class="n">qs</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># Add the generated field to form._meta.fields if it&#39;s defined to make</span>
        <span class="c1"># sure validation isn&#39;t skipped on that field.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">initial_form_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_as_new</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">initial_form_count</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_construct_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_construct_form</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_as_new</span><span class="p">:</span>
            <span class="n">mutable</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="s1">&#39;_mutable&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="c1"># Allow modifying an immutable QueryDict.</span>
            <span class="k">if</span> <span class="n">mutable</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">form</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">_mutable</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="c1"># Remove the primary key from the form&#39;s data, we are only</span>
            <span class="c1"># creating new instances</span>
            <span class="n">form</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">form</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pk_field</span><span class="o">.</span><span class="n">name</span><span class="p">)]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="c1"># Remove the foreign key from the form&#39;s data</span>
            <span class="n">form</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">form</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span><span class="p">)]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">mutable</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">form</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">_mutable</span> <span class="o">=</span> <span class="n">mutable</span>

        <span class="c1"># Set the fk value here so that the form can do its validation.</span>
        <span class="n">fk_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">pk</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
            <span class="n">fk_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span>
            <span class="n">fk_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">fk_value</span><span class="p">,</span> <span class="s1">&#39;pk&#39;</span><span class="p">,</span> <span class="n">fk_value</span><span class="p">)</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">get_attname</span><span class="p">(),</span> <span class="n">fk_value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">form</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">get_default_prefix</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="bp">cls</span><span class="o">.</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">save_new</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1"># Ensure the latest copy of the related instance is present on each</span>
        <span class="c1"># form (it may have been saved after the formset was originally</span>
        <span class="c1"># instantiated).</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
        <span class="c1"># Use commit=False so we can assign the parent key afterwards, then</span>
        <span class="c1"># save the object.</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">pk_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">get_attname</span><span class="p">(),</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">pk_value</span><span class="p">,</span> <span class="s1">&#39;pk&#39;</span><span class="p">,</span> <span class="n">pk_value</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">commit</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="c1"># form.save_m2m() can be called via the formset later on if commit=False</span>
        <span class="k">if</span> <span class="n">commit</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="s1">&#39;save_m2m&#39;</span><span class="p">):</span>
            <span class="n">form</span><span class="o">.</span><span class="n">save_m2m</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">add_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_fields</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pk_field</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pk_field</span><span class="o">.</span><span class="n">name</span>
            <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;pk_field&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># The foreign key field might not be on the form, so we poke at the</span>
            <span class="c1"># Model field to get the label, since we need that for error messages.</span>
            <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span>
            <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="s1">&#39;label&#39;</span><span class="p">,</span> <span class="n">capfirst</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">))</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to_field&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span>

        <span class="c1"># If we&#39;re adding a new object, ignore a parent&#39;s auto-generated key</span>
        <span class="c1"># as it will be regenerated on the save request.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;to_field&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">to_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to_field&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">to_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span>
            <span class="k">if</span> <span class="n">to_field</span><span class="o">.</span><span class="n">has_default</span><span class="p">():</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">to_field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">InlineForeignKeyField</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_unique_error_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unique_check</span><span class="p">):</span>
        <span class="n">unique_check</span> <span class="o">=</span> <span class="p">[</span><span class="n">field</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">unique_check</span> <span class="k">if</span> <span class="n">field</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_unique_error_message</span><span class="p">(</span><span class="n">unique_check</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_get_foreign_key</span><span class="p">(</span><span class="n">parent_model</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">can_fail</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Finds and returns the ForeignKey from model to parent if there is one</span>
<span class="sd">    (returns None if can_fail is True and no such field exists). If fk_name is</span>
<span class="sd">    provided, assume it is the name of the ForeignKey field. Unless can_fail is</span>
<span class="sd">    True, an exception is raised if there is no ForeignKey from model to</span>
<span class="sd">    parent_model.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># avoid circular import</span>
    <span class="kn">from</span> <span class="nn">django.db.models</span> <span class="k">import</span> <span class="n">ForeignKey</span>
    <span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
    <span class="k">if</span> <span class="n">fk_name</span><span class="p">:</span>
        <span class="n">fks_to_parent</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">fk_name</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">fks_to_parent</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">fk</span> <span class="o">=</span> <span class="n">fks_to_parent</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">fk</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">)</span> <span class="ow">or</span> \
                    <span class="p">(</span><span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">!=</span> <span class="n">parent_model</span> <span class="ow">and</span>
                     <span class="n">fk</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">parent_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">()):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;fk_name &#39;</span><span class="si">%s</span><span class="s2">&#39; is not a ForeignKey to &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">fk_name</span><span class="p">,</span> <span class="n">parent_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">fks_to_parent</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; has no field named &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">)</span>
            <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Try to discover what the ForeignKey from model to parent_model is</span>
        <span class="n">fks_to_parent</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span>
                <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="n">parent_model</span> <span class="ow">or</span>
                <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="ow">in</span> <span class="n">parent_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">()</span>
            <span class="p">)</span>
        <span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">fks_to_parent</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">fk</span> <span class="o">=</span> <span class="n">fks_to_parent</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">fks_to_parent</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">can_fail</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; has no ForeignKey to &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                    <span class="n">parent_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; has more than one ForeignKey to &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                    <span class="n">parent_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
    <span class="k">return</span> <span class="n">fk</span>


<div class="viewcode-block" id="inlineformset_factory"><a class="viewcode-back" href="../../../ref/forms/models.html#django.contrib.admin.inlineformset_factory">[docs]</a><span class="k">def</span> <span class="nf">inlineformset_factory</span><span class="p">(</span><span class="n">parent_model</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="n">ModelForm</span><span class="p">,</span>
                          <span class="n">formset</span><span class="o">=</span><span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="n">fk_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">can_order</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                          <span class="n">can_delete</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">max_num</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">formfield_callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">widgets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">validate_max</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">localized_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">labels</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">help_texts</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error_messages</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">min_num</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">validate_min</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">field_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns an ``InlineFormSet`` for the given kwargs.</span>

<span class="sd">    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``</span>
<span class="sd">    to ``parent_model``.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fk</span> <span class="o">=</span> <span class="n">_get_foreign_key</span><span class="p">(</span><span class="n">parent_model</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="o">=</span><span class="n">fk_name</span><span class="p">)</span>
    <span class="c1"># enforce a max_num=1 when the foreign key to the parent model is unique.</span>
    <span class="k">if</span> <span class="n">fk</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
        <span class="n">max_num</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
        <span class="s1">&#39;formfield_callback&#39;</span><span class="p">:</span> <span class="n">formfield_callback</span><span class="p">,</span>
        <span class="s1">&#39;formset&#39;</span><span class="p">:</span> <span class="n">formset</span><span class="p">,</span>
        <span class="s1">&#39;extra&#39;</span><span class="p">:</span> <span class="n">extra</span><span class="p">,</span>
        <span class="s1">&#39;can_delete&#39;</span><span class="p">:</span> <span class="n">can_delete</span><span class="p">,</span>
        <span class="s1">&#39;can_order&#39;</span><span class="p">:</span> <span class="n">can_order</span><span class="p">,</span>
        <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="n">fields</span><span class="p">,</span>
        <span class="s1">&#39;exclude&#39;</span><span class="p">:</span> <span class="n">exclude</span><span class="p">,</span>
        <span class="s1">&#39;min_num&#39;</span><span class="p">:</span> <span class="n">min_num</span><span class="p">,</span>
        <span class="s1">&#39;max_num&#39;</span><span class="p">:</span> <span class="n">max_num</span><span class="p">,</span>
        <span class="s1">&#39;widgets&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="p">,</span>
        <span class="s1">&#39;validate_min&#39;</span><span class="p">:</span> <span class="n">validate_min</span><span class="p">,</span>
        <span class="s1">&#39;validate_max&#39;</span><span class="p">:</span> <span class="n">validate_max</span><span class="p">,</span>
        <span class="s1">&#39;localized_fields&#39;</span><span class="p">:</span> <span class="n">localized_fields</span><span class="p">,</span>
        <span class="s1">&#39;labels&#39;</span><span class="p">:</span> <span class="n">labels</span><span class="p">,</span>
        <span class="s1">&#39;help_texts&#39;</span><span class="p">:</span> <span class="n">help_texts</span><span class="p">,</span>
        <span class="s1">&#39;error_messages&#39;</span><span class="p">:</span> <span class="n">error_messages</span><span class="p">,</span>
        <span class="s1">&#39;field_classes&#39;</span><span class="p">:</span> <span class="n">field_classes</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="n">FormSet</span> <span class="o">=</span> <span class="n">modelformset_factory</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="n">FormSet</span><span class="o">.</span><span class="n">fk</span> <span class="o">=</span> <span class="n">fk</span>
    <span class="k">return</span> <span class="n">FormSet</span></div>


<span class="c1"># Fields #####################################################################</span>

<span class="k">class</span> <span class="nc">InlineForeignKeyField</span><span class="p">(</span><span class="n">Field</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A basic integer field that deals with validating the given value to a</span>
<span class="sd">    given parent instance in an inline.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">widget</span> <span class="o">=</span> <span class="n">HiddenInput</span>
    <span class="n">default_error_messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;invalid_choice&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The inline foreign key did not match the parent instance primary key.&#39;</span><span class="p">),</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_instance</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span> <span class="o">=</span> <span class="n">parent_instance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pk_field</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;pk_field&quot;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to_field</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;to_field&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;initial&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;initial&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span><span class="o">.</span><span class="n">pk</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;required&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">InlineForeignKeyField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">empty_values</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk_field</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="c1"># if there is no value act as we did before.</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span>
        <span class="c1"># ensure the we compare the values as equal types.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field</span><span class="p">:</span>
            <span class="n">orig</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">orig</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span><span class="o">.</span><span class="n">pk</span>
        <span class="k">if</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">!=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">orig</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;invalid_choice&#39;</span><span class="p">],</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid_choice&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent_instance</span>

    <span class="k">def</span> <span class="nf">has_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">False</span>


<span class="k">class</span> <span class="nc">ModelChoiceIterator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field</span> <span class="o">=</span> <span class="n">field</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">queryset</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">empty_label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">yield</span> <span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">empty_label</span><span class="p">)</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="c1"># Can&#39;t use iterator() when queryset uses prefetch_related()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span><span class="p">:</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">iterator</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">queryset</span><span class="p">:</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">empty_label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="mi">0</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">choice</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">obj</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">label_from_instance</span><span class="p">(</span><span class="n">obj</span><span class="p">))</span>


<div class="viewcode-block" id="ModelChoiceField"><a class="viewcode-back" href="../../../ref/forms/fields.html#django.contrib.admin.ModelChoiceField">[docs]</a><span class="k">class</span> <span class="nc">ModelChoiceField</span><span class="p">(</span><span class="n">ChoiceField</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A ChoiceField whose choices are a model QuerySet.&quot;&quot;&quot;</span>
    <span class="c1"># This class is a subclass of ChoiceField for purity, but it doesn&#39;t</span>
    <span class="c1"># actually use any of ChoiceField&#39;s implementation.</span>
    <span class="n">default_error_messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;invalid_choice&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Select a valid choice. That choice is not one of&#39;</span>
                            <span class="s1">&#39; the available choices.&#39;</span><span class="p">),</span>
    <span class="p">}</span>
    <span class="n">iterator</span> <span class="o">=</span> <span class="n">ModelChoiceIterator</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">empty_label</span><span class="o">=</span><span class="s2">&quot;---------&quot;</span><span class="p">,</span>
                 <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">help_text</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">to_field_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">required</span> <span class="ow">and</span> <span class="p">(</span><span class="n">initial</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">empty_label</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">empty_label</span> <span class="o">=</span> <span class="n">empty_label</span>

        <span class="c1"># Call Field instead of ChoiceField __init__() because we don&#39;t need</span>
        <span class="c1"># ChoiceField.__init__().</span>
        <span class="n">Field</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">required</span><span class="p">,</span> <span class="n">widget</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">initial</span><span class="p">,</span> <span class="n">help_text</span><span class="p">,</span>
                       <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">limit_choices_to</span> <span class="o">=</span> <span class="n">limit_choices_to</span>   <span class="c1"># limit the queryset later.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to_field_name</span> <span class="o">=</span> <span class="n">to_field_name</span>

    <span class="k">def</span> <span class="nf">get_limit_choices_to</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns ``limit_choices_to`` for this form field.</span>

<span class="sd">        If it is a callable, it will be invoked and the result will be</span>
<span class="sd">        returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit_choices_to</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ChoiceField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__deepcopy__</span><span class="p">(</span><span class="n">memo</span><span class="p">)</span>
        <span class="c1"># Need to force a new ModelChoiceIterator to be created, bug #11183</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">result</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">result</span>

    <span class="k">def</span> <span class="nf">_get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_queryset</span>

    <span class="k">def</span> <span class="nf">_set_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_queryset</span> <span class="o">=</span> <span class="n">queryset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">choices</span>

    <span class="n">queryset</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_queryset</span><span class="p">,</span> <span class="n">_set_queryset</span><span class="p">)</span>

    <span class="c1"># this method will be used to create object labels by the QuerySetIterator.</span>
    <span class="c1"># Override it to customize the label.</span>
    <span class="k">def</span> <span class="nf">label_from_instance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method is used to convert objects into strings; it&#39;s used to</span>
<span class="sd">        generate the labels for the choices presented by this object. Subclasses</span>
<span class="sd">        can override this method to customize the display of the choices.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># If self._choices is set, then somebody must have manually set</span>
        <span class="c1"># the property self.choices. In this case, just return self._choices.</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_choices&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_choices</span>

        <span class="c1"># Otherwise, execute the QuerySet in self.queryset to determine the</span>
        <span class="c1"># choices dynamically. Return a fresh ModelChoiceIterator that has not been</span>
        <span class="c1"># consumed. Note that we&#39;re instantiating a new ModelChoiceIterator *each*</span>
        <span class="c1"># time _get_choices() is called (and, thus, each time self.choices is</span>
        <span class="c1"># accessed) so that we can ensure the QuerySet has not been consumed. This</span>
        <span class="c1"># construct might look complicated but it allows for lazy evaluation of</span>
        <span class="c1"># the queryset.</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">iterator</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="n">choices</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_choices</span><span class="p">,</span> <span class="n">ChoiceField</span><span class="o">.</span><span class="n">_set_choices</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">prepare_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field_name</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">serializable_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to_field_name</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">pk</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ModelChoiceField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">empty_values</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field_name</span> <span class="ow">or</span> <span class="s1">&#39;pk&#39;</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">value</span><span class="p">})</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;invalid_choice&#39;</span><span class="p">],</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid_choice&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Field</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">disabled</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">initial_value</span> <span class="o">=</span> <span class="n">initial</span> <span class="k">if</span> <span class="n">initial</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
        <span class="n">data_value</span> <span class="o">=</span> <span class="n">data</span> <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
        <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">initial_value</span><span class="p">))</span> <span class="o">!=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">data_value</span><span class="p">)</span></div>


<div class="viewcode-block" id="ModelMultipleChoiceField"><a class="viewcode-back" href="../../../ref/forms/fields.html#django.contrib.admin.ModelMultipleChoiceField">[docs]</a><span class="k">class</span> <span class="nc">ModelMultipleChoiceField</span><span class="p">(</span><span class="n">ModelChoiceField</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A MultipleChoiceField whose choices are a model QuerySet.&quot;&quot;&quot;</span>
    <span class="n">widget</span> <span class="o">=</span> <span class="n">SelectMultiple</span>
    <span class="n">hidden_widget</span> <span class="o">=</span> <span class="n">MultipleHiddenInput</span>
    <span class="n">default_error_messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;list&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a list of values.&#39;</span><span class="p">),</span>
        <span class="s1">&#39;invalid_choice&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Select a valid choice. </span><span class="si">%(value)s</span><span class="s1"> is not one of the&#39;</span>
                            <span class="s1">&#39; available choices.&#39;</span><span class="p">),</span>
        <span class="s1">&#39;invalid_pk_value&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;&quot;</span><span class="si">%(pk)s</span><span class="s1">&quot; is not a valid value for a primary key.&#39;</span><span class="p">)</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">initial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">help_text</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ModelMultipleChoiceField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">queryset</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">required</span><span class="p">,</span> <span class="n">widget</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">initial</span><span class="p">,</span> <span class="n">help_text</span><span class="p">,</span>
            <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_values</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">required</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;required&#39;</span><span class="p">],</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;required&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">required</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">none</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;list&#39;</span><span class="p">],</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;list&#39;</span><span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_check_values</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="c1"># Since this overrides the inherited ModelChoiceField.clean</span>
        <span class="c1"># we run custom validators here</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">run_validators</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">qs</span>

    <span class="k">def</span> <span class="nf">_check_values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given a list of possible PK values, returns a QuerySet of the</span>
<span class="sd">        corresponding objects. Raises a ValidationError if a given value is</span>
<span class="sd">        invalid (not a valid PK, not in the queryset, etc.)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field_name</span> <span class="ow">or</span> <span class="s1">&#39;pk&#39;</span>
        <span class="c1"># deduplicate given values to avoid creating many querysets or</span>
        <span class="c1"># requiring the database backend deduplicate efficiently.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="c1"># list of lists isn&#39;t hashable, for example</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;list&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;list&#39;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">for</span> <span class="n">pk</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">pk</span><span class="p">})</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;invalid_pk_value&#39;</span><span class="p">],</span>
                    <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid_pk_value&#39;</span><span class="p">,</span>
                    <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;pk&#39;</span><span class="p">:</span> <span class="n">pk</span><span class="p">},</span>
                <span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__in&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">:</span> <span class="n">value</span><span class="p">})</span>
        <span class="n">pks</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">o</span><span class="p">,</span> <span class="n">key</span><span class="p">))</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">qs</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">force_text</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">pks</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;invalid_choice&#39;</span><span class="p">],</span>
                    <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid_choice&#39;</span><span class="p">,</span>
                    <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="n">val</span><span class="p">},</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">qs</span>

    <span class="k">def</span> <span class="nf">prepare_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="s1">&#39;__iter__&#39;</span><span class="p">)</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[</span><span class="nb">super</span><span class="p">(</span><span class="n">ModelMultipleChoiceField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ModelMultipleChoiceField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">disabled</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">initial</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">initial</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">initial</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">initial_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepare_value</span><span class="p">(</span><span class="n">initial</span><span class="p">))</span>
        <span class="n">data_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">data</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data_set</span> <span class="o">!=</span> <span class="n">initial_set</span></div>


<span class="k">def</span> <span class="nf">modelform_defines_fields</span><span class="p">(</span><span class="n">form_class</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">form_class</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="p">(</span>
            <span class="nb">hasattr</span><span class="p">(</span><span class="n">form_class</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="n">form_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span>
             <span class="n">form_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">)</span>
            <span class="p">))</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>